<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/list-to-detail.css" />
		<style>
			a {
				word-break: break-word;
				width:100%;
			}
		</style>
	</head>

	<body>

		<div class="mui-content">
			<!--顶部banner图 开始-->
			<div id="kr-article-banner" class="kr-article-banner">
				<div id="kr-article-cover" class="kr-article-cover">
					<img :src="cover">
				</div>
				<h2 id="kr-article-title" class="kr-article-title">{{title}}</h2>
			</div>
			<!--顶部banner图 结束-->

			<div class="kr-article-content">
				<!-- 文章作者、发布时间等信息 -->
				<div class="kr-article-meta">
					<div class="kr-article-text">发表于</div>
					<div id="kr-article-time" class="kr-article-time">{{time}}</div>
				</div>
				<!--文章详细内容-->
				<div id="kr-article-article" class="kr-article-article" v-html="content"></div>
			</div>
		</div>

		<script src="../js/mui.min.js"></script>
		<script src="../js/vue.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="../js/public.js"></script>
		<script type="text/javascript">
			mui.init();

			function getDefaultData() {
				return {
					cover: '',
					title: '',
					author: '',
					time: '',
					content: ''
				}
			}

			var vm = new Vue({
				el: '.mui-content',
				data: getDefaultData(),
				methods: {
					resetData: function() { //重置数据
						Object.assign(this.$data, getDefaultData());
					}
				}
			});

			//监听自定义事件，获取新闻详情
			document.addEventListener('get_detail', function(event) {
				var guid = event.detail.guid;
				if(!guid) {
					return;
				}

				//前页传入的数据，直接渲染，无需等待ajax请求详情后
				vm.cover = event.detail.cover;
				vm.title = event.detail.title;
				vm.author = event.detail.author;
				vm.time = event.detail.time;

				var mdata = {
					"id": guid
				};
				mui.postParam(GetNewsDetail, mdata, function(result) {
					mConsole.log("新闻详情：" + JSON.stringify(result));
					if(result.code == 0) {
						if(result.news.content && result.news.content.length > 0) {
							vm.content = result.news.content.replace(/(\r\n)|(\n)/g,'<br/>').replace(/ /g,'&nbsp;');
							vm.content = replaceTag(vm.content);
						}
						return;
					}
					mymui(result.msg);
				});

			});

			//重写返回逻辑
			mui.back = function() {
				plus.webview.currentWebview().hide("auto", 300);
			}

			//窗口隐藏时，重置页面数据
			mui.plusReady(function() {
				var self = plus.webview.currentWebview();
				self.addEventListener("hide", function(e) {
					window.scrollTo(0, 0);
					vm.resetData();
				}, false);
			});
			
			// 替换标签
			function replaceTag(s) {
				var index = s.indexOf("[img=");
				var link = "";
				if(index != -1) {
					link = s.substring(index + 5, s.indexOf("]", index));
					s = s.replace("[img=" + link + "]","<img src='" + link + "'/>");
					s = replaceTag(s);
				}
				
				index = s.indexOf("[a=");
				if(index != -1) {
					link = s.substring(index + 3, s.indexOf("]", index));
					s = s.replace("[a=" + link + "]","<a href=\"javascript:plus.runtime.openURL('" + link + "')\">" + link + "</a>");
					s = replaceTag(s);
				}
				
				return s;
			}
		</script>
	</body>

</html>